package com.devops.controller;

import com.alibaba.excel.EasyExcel;
import com.devops.entity.dto.ExcelDataListener;
import com.devops.entity.dto.UploadExcelDTO;
import com.devops.entity.po.OrderInfoPO;
import com.devops.service.TestDevopsService;
import com.devops.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: mis_wu
 * @Company: asia info
 * @Create_time: 2023-10-12
 * @Description: 测试
 */
@RestController
@RequestMapping("/testApis")
@CrossOrigin
public class TestController {

    @Value("${server.port}")
    private Integer port;

    @Resource
    private TestDevopsService testDevopsService;

    @GetMapping("/testApi1")
    public ResponseResult<String> testApi1(){
        return ResponseResult.isSuccess("接口请求成功,端口：" + port);
    }

    @GetMapping("/testApi2")
    public ResponseResult<String> testApi2(Integer id){
        return ResponseResult.isSuccess("接口请求成功,入参：" + id);
    }

    @GetMapping("/testApi3")
    public ResponseResult<String> testApi3(){
        return ResponseResult.error(10001,"这是一个错误请求，请重试！！！");
    }

    @GetMapping("/testApi4")
    public ResponseResult<String> testApi4(){
        return ResponseResult.isSuccess("这是一个代理请求，返回成功");
    }

    @GetMapping("/testApi5")
    public ResponseResult<String> testApi5(String name){
        try {
            return testDevopsService.testTransactional(name);
        }catch (Exception e){
            return ResponseResult.error(10001,"事务回滚" + e.getMessage());
        }
    }


    @GetMapping("/test/devops/testApi6/{status}")
    public ResponseResult<String> testApi6(@PathVariable("status") String status,@RequestParam Long id){

        return ResponseResult.isSuccess("nginx代理测试");
    }


    @PostMapping("/uploadExcel")
    public ResponseResult<String> uploadExcel(MultipartFile file){

        try {
            EasyExcel.read(file.getInputStream(), UploadExcelDTO.class,new ExcelDataListener()).sheet().headRowNumber(2).doRead();
        }catch (Exception e){
            e.printStackTrace();
        }


        return ResponseResult.isSuccess();
    }

    @PostMapping("/uploadFileTest")
    public ResponseResult<String> uploadFileTest(@RequestPart MultipartFile file, @RequestPart List<OrderInfoPO> pos){
        System.out.println("文件信息：" + file.getOriginalFilename());
        System.out.println("实体数据：" + pos);
        return ResponseResult.isSuccess();
    }


}
